package com.runachina.common;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.runachina.R;
import com.runachina.othercompany.EuropeCompanyActivity;
import com.runachina.othercompany.HuizhongActivity;
import com.runachina.othercompany.OtherCompanyActivity;
import com.runachina.runamodbus.RunaModusActivity;
import com.runachina.runa.ReadMeterActivity;
import com.runachina.runa.RunaValveActivity;

public class CompanyActivity extends BaseActivity implements OnClickListener {

    private Toolbar mToolbar;
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;

    private RadioGroup radioGroup;
    private RadioButton runa188, runamodbus, runafamen,yilinvalve,tem_collector;
    private Button btnhongwai, btnmbus, btn485;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_company);

        /*set toolbar title*/
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setTitle("抄表方式");
        mToolbar.setSubtitle("");
        mToolbar.setTitleTextColor(Color.WHITE);
        mToolbar.setSubtitleTextColor(Color.WHITE);
        if (mToolbar != null) {
            setSupportActionBar(mToolbar);
        }
        getSupportActionBar().setDisplayShowTitleEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//设置home，返回菜单

        init_radioButton();

        set_radioBtton_status();

        init_btn();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.runa188:
                companyName = COMPANYNAMEARR[0];//"瑞纳--抄表188";
                break;
            case R.id.runamodbus:
                companyName = COMPANYNAMEARR[1];//"瑞纳--抄表modbus";
                break;
            case R.id.runafamen:
                companyName = COMPANYNAMEARR[2];//"瑞纳--阀门";
                break;
//            case R.id.zhengyu:
//                companyName = COMPANYNAMEARR[3];//"震宇--抄表";
//                break;
//            case R.id.shengli:
//                companyName = COMPANYNAMEARR[4];//"胜利--抄表";
//                break;
//            case R.id.lichuang:
//                companyName = COMPANYNAMEARR[5];//"力创--抄表";
//                break;
//            case R.id.lanjier:
//                companyName = COMPANYNAMEARR[6];//"兰吉尔--抄表";
//                break;
//            case R.id.huizhong:
//                companyName = COMPANYNAMEARR[7];//"汇中--抄表";
//                break;
//            case R.id.weian:
//                companyName = COMPANYNAMEARR[8];//"伟岸--抄表";
//                break;
//            case R.id.hualeyi:
//                companyName = COMPANYNAMEARR[9];//"华仪乐--抄表";
//                break;
//            case R.id.maituo:
//                companyName = COMPANYNAMEARR[10];//"迈拓--抄表";
//                break;
//            case R.id.tiangang:
//                companyName = COMPANYNAMEARR[11];//"天罡--抄表";
            case R.id.yilin:
                companyName = COMPANYNAMEARR[3];//"亿林--阀门";
                break;
            case R.id.tem_collector:
                companyName = COMPANYNAMEARR[4];//"瑞纳--温度采集器";
                break;
            //下面是3个btn: {"mbus","红外","485"}
            case R.id.btnmbus:
                readMethod = READMETHOD[0];
                startIntent();
                break;
            case R.id.btnhongwai:
                readMethod = READMETHOD[1];
                startIntent();
                break;
            case R.id.btn485:
                readMethod = READMETHOD[2];
                startIntent();
                break;
            default:
                break;
        }
    }

    private void init_btn() {
        btnhongwai = (Button) findViewById(R.id.btnhongwai);
        btnhongwai.setOnClickListener(this);
        btnmbus = (Button) findViewById(R.id.btnmbus);
        btnmbus.setOnClickListener(this);
        btn485 = (Button) findViewById(R.id.btn485);
        btn485.setOnClickListener(this);
    }

    void init_radioButton() {
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);

        runa188 = (RadioButton) findViewById(R.id.runa188);
        runa188.setOnClickListener(this);
        runamodbus = (RadioButton) findViewById(R.id.runamodbus);
        runamodbus.setOnClickListener(this);
        runafamen = (RadioButton) findViewById(R.id.runafamen);
        runafamen.setOnClickListener(this);
//        zhengyu = (RadioButton) findViewById(R.id.zhengyu);
//        zhengyu.setOnClickListener(this);
//        shengli = (RadioButton) findViewById(R.id.shengli);
//        shengli.setOnClickListener(this);
//        lichuang = (RadioButton) findViewById(R.id.lichuang);
//        lichuang.setOnClickListener(this);
//        lanjier = (RadioButton) findViewById(R.id.lanjier);
//        lanjier.setOnClickListener(this);
//        huizhong = (RadioButton) findViewById(R.id.huizhong);
//        huizhong.setOnClickListener(this);
//        weian = (RadioButton) findViewById(R.id.weian);
//        weian.setOnClickListener(this);
//        hualeyi = (RadioButton) findViewById(R.id.hualeyi);
//        hualeyi.setOnClickListener(this);
//        maituo = (RadioButton) findViewById(R.id.maituo);
//        maituo.setOnClickListener(this);
//        tiangang = (RadioButton) findViewById(R.id.tiangang);
//        tiangang.setOnClickListener(this);
        yilinvalve= (RadioButton) findViewById(R.id.yilin);
        yilinvalve.setOnClickListener(this);
        tem_collector= (RadioButton) findViewById(R.id.tem_collector);
        tem_collector.setOnClickListener(this);
    }

    private void set_radioBtton_status() {
        /*首先找到当前选择的是哪一个company：使用companyNameArr是为了以后方便修改和添加*/
        int id = 0;
        for (int i = 0; i < COMPANYNAMEARR.length; i++) {
            if (companyName.equals(COMPANYNAMEARR[i])) {
                id = i;
                i = COMPANYNAMEARR.length;
            }
        }
        /*然后设置RadioBtn对应的状态*/
        switch (id) {
            case 0:
                runa188.setChecked(true);
                break;
            case 1:
                runamodbus.setChecked(true);
                break;
            case 2:
                runafamen.setChecked(true);
                break;
            case 3:
                yilinvalve.setChecked(true);
                break;
            case 4:
                tem_collector.setChecked(true);
                break;
//            case 3:
//                zhengyu.setChecked(true);
//                break;
//            case 4:
//                shengli.setChecked(true);
//                break;
//            case 5:
//                lichuang.setChecked(true);
//                break;
//            case 6:
//                lanjier.setChecked(true);
//                break;
//            case 7:
//                huizhong.setChecked(true);
//                break;
//            case 8:
//                weian.setChecked(true);
//                break;
//            case 9:
//                hualeyi.setChecked(true);
//                break;
//            case 10:
//                maituo.setChecked(true);
//                break;
//            case 11:
//                tiangang.setChecked(true);
//                break;
            default:
                break;
        }
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                startIntent();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    /**
     * 开始跳转
     * 并且在此指定侧边栏功能的个数
     */
    private void startIntent() {
//        if (companyName.equals("瑞纳--阀门")) {
//            show("暂不支持");
//        } else {
            Class<?> ac;
            switch (companyName) {
                case "瑞纳--抄表(188)":
                    ac = ReadMeterActivity.class;
                    break;
                case "瑞纳--抄表(modbus)":
                    ac = RunaModusActivity.class;
                    break;
                case "瑞纳--阀门":
                    ac = com.runachina.runavalve.ReadMeterActivity.class;
                    break;
                case "亿林--阀门":
                    ac = com.runachina.yilinvalve.YiLinValveReadMeterActivity.class;
                    break;
                case "瑞纳--温度采集器":
                    ac = com.runachina.temcollector.TemReadMeterActivity.class;
                    break;
//                case "兰吉尔--抄表":
//                    ac = EuropeCompanyActivity.class;
//                    break;
//                case "伟岸--抄表":
//                    ac = EuropeCompanyActivity.class;
//                    break;
//                case "汇中--抄表":
//                    ac = HuizhongActivity.class;
//                    break;
                default:
                    ac = OtherCompanyActivity.class;
                    break;
            }
            intent = new Intent(CompanyActivity.this, ac);
            startActivity(intent);
            finish();
//        }
    }

    /**
     * 设置back监听
     */
    @Override
    public void onBackPressed() {
        exitApp();
    }
}


